﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Collections;
using SecureCommander.Helper;

namespace SecureCommander.Core
{
    public class Disk 
    {
        public string Name { get; set; }
        public string CurrentPath { get; set; }

        public Disk()
        {
            Name = "";
            CurrentPath = "";
        }

        public Disk(string name, string currentPath)
        {
            Name = name;
            CurrentPath = currentPath;
        }
    }

    public class ConfigurationData
    {
        private static string CONFIG_DATA_FILE_NAME = "config.xml";
        public DateTime LastUpdatedDate{ get; set; }
        public List<Disk> CachePaths { get; set; }
        public string Partition1 { get; set; }
        public string Partition2 { get; set; }
        
        public ConfigurationData()
        {
            CachePaths = new List<Disk>();
            Partition1 = Partition2 = "";
        }

        public void Save()
        {
            try
            {
                LastUpdatedDate = DateTime.Today;

                XmlSerializer serializer = new XmlSerializer(typeof(ConfigurationData));
                TextWriter textWriter = new StreamWriter(CONFIG_DATA_FILE_NAME);
                serializer.Serialize(textWriter, this);
                textWriter.Close();
            }
            catch (Exception e)
            {
                FileHelper.ShowMessage("Can not save user data", System.Windows.MessageBoxImage.Warning);
            }
        }

        public void UpdateCachePaths(Hashtable cachePaths)
        {
            CachePaths.Clear();
            foreach (DictionaryEntry item in cachePaths)
            {
                CachePaths.Add(new Disk(item.Key.ToString(), item.Value.ToString()));
            }
        }

        public Hashtable getCachePathsAsHashtable()
        {
            Hashtable rs = new Hashtable();
            foreach (Disk item in CachePaths)
            {
                rs.Add(item.Name, item.CurrentPath);
            }
            return rs;
        }

        public static ConfigurationData Load()
        {
            ConfigurationData config = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(ConfigurationData));
                TextReader textReader = new StreamReader(CONFIG_DATA_FILE_NAME);
                config = (ConfigurationData)serializer.Deserialize(textReader);
                textReader.Close();
            }
            catch
            {
                config = new ConfigurationData();
                config.LastUpdatedDate = DateTime.Today;
                
                string[] disks = Environment.GetLogicalDrives();
                foreach (string disk in disks)
                {
                    config.CachePaths.Add(new Disk(disk, disk));
                }
                config.Partition1 = config.Partition2 = disks[0];
            }
            return config;
        }
    }


}
